package com.pangu.ui.adapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ExpandableListAdapter;

/**
 * 2011-5-26 软件分组适配器<br>
 * 注：分组全部都是展开的并且不能收缩
 * 
 * @author cxlin
 * 
 */

public abstract class AGroupItemAdapter<T> extends BaseAdapter implements ExpandableListAdapter {

	protected static final String TAG = "AGroupSoftwareAdapter";
	public static final String NONE_TITLE = "0";
	// 数据分组列表
	private LinkedHashMap<String, List<T>> expandTable;

	public AGroupItemAdapter() {
		expandTable = new LinkedHashMap<String, List<T>>();
	}

	/**
	 * 初始化适配器
	 */
	public final void initItemGroup() {
		// 清除旧的数据
		expandTable.clear();

		// 初始化数据
		List<T> baseItemList = getItemList();
		List<T> tempItemList = null;
		String shortName = null;
		for (int i = 0, size = baseItemList.size(); i < size; i++) {
			String groupName = getItemGroupName(i);
			shortName = (groupName == null ? "0" : groupName);
			tempItemList = expandTable.get(shortName);
			if (tempItemList == null) {
				tempItemList = new ArrayList<T>();
				expandTable.put(shortName, tempItemList);
			}
			tempItemList.add(baseItemList.get(i));

		}
	}

	abstract protected String getItemGroupName(int position);

	/**
	 * 获得数据项
	 * 
	 * @return
	 */
	protected abstract List<T> getItemList();

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		Set<String> keySet = expandTable.keySet();
		int index = 0;
		String useKey = null;
		for (String key : keySet) {
			if (index == groupPosition) {
				useKey = key;
				break;
			}
			index++;
		}
		return (useKey == null ? null : expandTable.get(useKey).get(childPosition));
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		Set<String> keySet = expandTable.keySet();
		int index = 0;
		String useKey = null;
		for (String key : keySet) {
			if (index == groupPosition) {
				useKey = key;
				break;
			}
			index++;
		}
		return (useKey == null ? 0 : expandTable.get(useKey).size());
	}

	@Override
	public Object getGroup(int groupPosition) {
		Set<String> keySet = expandTable.keySet();
		int index = 0;
		String useKey = null;
		for (String key : keySet) {
			if (index == groupPosition) {
				useKey = key;
				break;
			}
			index++;
		}
		return useKey;
	}

	@Override
	public int getGroupCount() {
		return expandTable.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	public boolean areAllItemsEnabled() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public void onGroupCollapsed(int groupPosition) {
	}

	public void onGroupExpanded(int groupPosition) {
	}

	/**
	 * Override this method if you foresee a clash in IDs based on this scheme:
	 * <p>
	 * Base implementation returns a long:
	 * <li>bit 0: Whether this ID points to a child (unset) or group (set), so
	 * for this method this bit will be 1.
	 * <li>bit 1-31: Lower 31 bits of the groupId
	 * <li>bit 32-63: Lower 32 bits of the childId.
	 * <p>
	 * {@inheritDoc}
	 */
	public long getCombinedChildId(long groupId, long childId) {
		return 0x8000000000000000L | ((groupId & 0x7FFFFFFF) << 32) | (childId & 0xFFFFFFFF);
	}

	/**
	 * Override this method if you foresee a clash in IDs based on this scheme:
	 * <p>
	 * Base implementation returns a long:
	 * <li>bit 0: Whether this ID points to a child (unset) or group (set), so
	 * for this method this bit will be 0.
	 * <li>bit 1-31: Lower 31 bits of the groupId
	 * <li>bit 32-63: Lower 32 bits of the childId.
	 * <p>
	 * {@inheritDoc}
	 */
	public long getCombinedGroupId(long groupId) {
		return (groupId & 0x7FFFFFFF) << 32;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean isEmpty() {
		return getGroupCount() == 0;
	}

	// ////////////////// 以下是不要的方法 ////////////////////
	@Override
	final public int getCount() {
		throw new UnsupportedOperationException("please use LoadableList getMAdapter()");
	}

	@Override
	final public Object getItem(int position) {
		throw new UnsupportedOperationException("please use LoadableList getMAdapter()");
	}

	@Override
	final public long getItemId(int position) {
		throw new UnsupportedOperationException("please use LoadableList getMAdapter()");
	}

	@Override
	final public View getView(int position, View convertView, ViewGroup parent) {
		throw new UnsupportedOperationException("please use LoadableList getMAdapter()");
	}

}
